
package com.city.ajax.party;

import com.city.domain.party.*;
import com.city.domain.party.format.Abroad;
import com.city.domain.pub.*;
import com.city.service.party.*;
import com.city.service.pub.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ajax/party")
public class partyAjax {
    @Autowired
    private SaveService saveService;

    @Autowired
    private PartyCadreLevelService partyCadreLevelService;

    @Autowired
    private PubPoliticalOutlookService pubPoliticalOutlookService;

    @Autowired
    private PubFamilyRelationshipService familyRelationshipService;

    @Autowired
    private PubCountryService pubCountryService;

    @Autowired
    private PartyFabroadService partyFabroadService;

    @Autowired
    private ReportPersonService reportPersonService;

    @Autowired
    private ReportGroupService reportGroupService;

    @Autowired
    private PubArticleService pubArticleService;

    @Autowired
    private PubNationCategoryService pubNationCategoryService;

    @Autowired
    private PubEducationDegreeService pubEducationDegreeService;

    @Autowired
    private PubOccupationService pubOccupationService;

    @Autowired
    private PubIndustryCategoryService pubIndustryCategoryService;

    @Autowired
    private PartyVideoService videoService;

    @Autowired
    private PartyLessonService lessonService;
    /**
     **
      *@author:WY
      *@date: 2018/7/18 20:12
      *@description:保存提交的转入转出信息
     *
     */
    @PostMapping("/saveInfo")
    @ResponseBody
    public Map<String,Object> getInfo(
                                      @Param("nowGroup") String nowGroup,
                                      @Param("toGroup")String toGroup,
                                      @Param("payTime") String payTime,
                                      @Param("personalTel") String personalTel,
                                      @Param("reason") String reason,
                                      @Param("name") String name){
        Map<String,Object> map = new HashMap<>();
        PartyOrelationship partyOrelationship = new PartyOrelationship();
//        partyOrelationship.setId(1432425);
        partyOrelationship.setName(name);
        partyOrelationship.setSourceBranch(nowGroup);
        partyOrelationship.setTciBranch(toGroup);
        partyOrelationship.setBranchPaymentTime(payTime);
        partyOrelationship.setTellphone(personalTel);
        partyOrelationship.setReason(reason);
        Long date = new Date().getTime();
//        SimpleDateFormat format = new SimpleDateFormat();
//        String s = format.format(date);
        String s = String.valueOf(date);
        partyOrelationship.setTime(s);
        saveService.save(partyOrelationship);

        map.put("code",0);

        return map;
    }

    /**
     * *
      *@author:WY
      *@date: 2018/7/19 10:33
      *@description:保存流动申请
     *
     */
    @PostMapping("/saveliudong")
    @ResponseBody
    public Map<String,Object> getLiudong(@Param("name") String name,
                                         @Param("idCard") String idCard,
                                         @Param("telephone") String telephone,
                                         @Param("branch") String branch,
                                         @Param("reason") String reason,
                                         @Param("type") String type,
                                         @Param("sourceOccupation") String sourceOccupation,
                                         @Param("sourceBranchName") String sourceBranchName,
                                         @Param("sourcePerson") String sourcePerson,
                                         @Param("sourcePersonTell") String sourcePersonTell,
                                         @Param("explain") String explain){
        Map<String,Object> map = new HashMap<>();
        PartyFahu partyFahu = new PartyFahu();
        partyFahu.setName(name);
        partyFahu.setIdCard(idCard);
        partyFahu.setTellphone(telephone);
        partyFahu.setBranch(branch);
        partyFahu.setReason(reason);
        partyFahu.setType(type);
        partyFahu.setSourceOccupation(sourceOccupation);
        partyFahu.setSourceBranchName(sourceBranchName);
        partyFahu.setSourcePerson(sourcePerson);
        partyFahu.setSourcePersonTell(sourcePersonTell);
        partyFahu.setpExplain(explain);

        Long date = new Date().getTime();
        String s = String.valueOf(date);
        partyFahu.setTime(s);

        saveService.save(partyFahu);

        map.put("code",0);
        return map;
    }

    /**
     * *
      *@author:WY
      *@date: 2018/7/19 13:56
      *@description:保存流出申请
     *
     */
    @PostMapping("/saveliuchu")
    @ResponseBody
    public Map<String,Object> getLiuchu(@Param("name") String name,
                                        @Param("sourceBranch") String sourceBranch,
                                        @Param("flowoutBranch") String flowoutBranch,
                                        @Param("flowoutPerson") String flowoutPerson,
                                        @Param("flowoutTell") String flowoutTell,
                                        @Param("type") String type,
                                        @Param("reason") String reason,
                                        @Param("pExplain") String pExplain,
                                        @Param("sourceOccupation") String sourceOccupation){
        Map<String,Object> map = new HashMap<>();
        PartyFlowout partyFlowout = new PartyFlowout();
        partyFlowout.setName(name);
        partyFlowout.setSourceBranch(sourceBranch);
        partyFlowout.setFlowoutBranch(flowoutBranch);
        partyFlowout.setFlowoutPerson(flowoutPerson);
        partyFlowout.setFlowoutTell(flowoutTell);
        partyFlowout.setType(type);
        partyFlowout.setReason(reason);
        partyFlowout.setpExplain(pExplain);
        partyFlowout.setSourceOccupation(sourceOccupation);
        long date = new Date().getTime();
        String s = String.valueOf(date);
        partyFlowout.setTime(s);

        saveService.save(partyFlowout);
        map.put("code",0);
        return map;
    }

    /**
     **
      *@author:WY
      *@date: 2018/7/19 16:08
      *@description:级别
     *
     */
    @PostMapping("/levels")
    @ResponseBody
    public Map<String,Object> getLevels(@Param("check") int check){
        Map<String,Object> map = new HashMap<>();
        if(check==1024){
            map.put("code",0);
            List<PartyCadreLevel> partyCadreLevels = partyCadreLevelService.findAll();
            map.put("data",partyCadreLevels);
        }
        else {
            System.out.println(check);
        }
        return map;
    }

    /**
     **
      *@author:WY
      *@date: 2018/7/19 19:34
      *@description:政治身份
     *
     */
    @PostMapping("/identity")
    @ResponseBody
    public Map<String,Object> getIdentity(@Param("check") int check){
        Map<String,Object> map = new HashMap<>();
        if(check==1024){
            List<PubPoliticalOutlook> pubs = pubPoliticalOutlookService.findAll();
            map.put("code",0);
            map.put("data",pubs);
        }
        return map;
    }
    /**
     **
      *@author:WY
      *@date: 2018/7/19 19:59
      *@description:家庭成员下拉框
     */
    @PostMapping("/family")
    @ResponseBody
    public Map<String,Object> getFamily(@Param("check") int check){
        Map<String,Object> map = new HashMap<>();
        if(check==1024){
           List<PubFamilyRelationship> familyRelationships = familyRelationshipService.findALL();
           map.put("code",0);
           map.put("data",familyRelationships);
        }


        return map;
    }

    /**
     * *
      *@author:WY
      *@date: 2018/7/20 10:18
      *@description:国家分类
     *
     */
    @PostMapping("/countries")
    @ResponseBody
    public Map<String,Object> getCountry(@Param("check") int check){
        Map<String,Object> map = new HashMap<>();
        if(check==1024){
            List<PubCountry> pubCountries = pubCountryService.findAll();
            map.put("code",0);
            map.put("data",pubCountries);
        }
        return map;
    }

    /**
      *@author:WY
      *@date: 2018/7/24 16:10
      *@description:民族分类
     */
    @PostMapping("/nations")
    @ResponseBody
    public Map<String,Object> getNations(@Param("check") int check){
        Map<String,Object> map = new HashMap<>();
        if(check==1024){
            List<PubNationCategory> pubNationCategories = pubNationCategoryService.getAll();
            map.put("code",0);
            map.put("data",pubNationCategories);
        }
        return map;
    }

    /***
     *@author:WY
     *@date: 2018/7/24 16:13
     *@description:学历分类
     */

    @PostMapping("/education_degree")
    @ResponseBody
    public Map<String,Object> getEducations(@Param("check") int check){
        Map<String,Object> map = new HashMap<>();
        if(check==1024){
            List<PubEducationDegree> pubEducationDegrees = pubEducationDegreeService.getAll();
            map.put("code",0);
            map.put("data",pubEducationDegrees);
        }        return map;
    }

    /**
      *@author:Wy
      *@date: 2018/7/24 16:14
      *@description:职业分类
     */
    @PostMapping("/occupations")
    @ResponseBody
    public Map<String,Object> getOccupations(@Param("check") int check){
        Map<String,Object> map = new HashMap<>();
        if(check==1024){
            List<PubOccupation> occupations = pubOccupationService.findAll();
            map.put("code",0);
            map.put("data",occupations);
        }
        return map;
    }

    /**
      *@author:WY
      *@date: 2018/7/24 16:14
     * @description:所属行业分类
     */
    @PostMapping("/industrys")
    @ResponseBody
    public Map<String,Object> getIndustrys(@Param("check") int check){
        Map<String,Object> map = new HashMap<>();
        if(check==1024){
            List<PubIndustryCategory> pubIndustryCategories = pubIndustryCategoryService.findAll();
            map.put("code",0);
            map.put("data",pubIndustryCategories);
        }
        return map;
    }


    /**
     **
      *@author:WY
      *@date: 2018/7/20 11:09
      *@description:干部出国申请提交
     */
    @PostMapping("/abroad")
    @ResponseBody
    public Map<String,Object> getAbroad(Abroad abroad){
        Map<String,Object> map = new HashMap<>();
        //得到显著表中的最高id，并设置max+1给即将添加的数据
//        List<PartyFabroad> partyFabroads = partyFabroadService.findAll();
//        int id = partyFabroads.size()+1;
        //将传过来的数据 分别封装到对应的表中
        //出国表

        /**
         **
          *@author:WY
          *@date: 2018/7/21 12:58
          *@description:不要问我代码为啥那么长，都是前端懒得封装的错
         *
         */
        if(abroad.getInName1().get(0).replace("\"","").replace("[","").replace("]","") == ""  || abroad.getInName1().get(0).replace("\"","").replace("[","").replace("]","").equals("")){
                map.put("code",1);
        }else{
            //出国表
            PartyFabroad partyFabroad = new PartyFabroad();
//            partyFabroad.setId(id);
            partyFabroad.setName(abroad.getAbdName());
            partyFabroad.setSex(abroad.getAbdSex());
            partyFabroad.setHealthy(abroad.getAbdHealth());
            partyFabroad.setCompany(abroad.getToGroup());
            partyFabroad.setPost(abroad.getAbdPost());
            partyFabroad.setLevel(abroad.getAbdLevel());
            partyFabroad.setPoliticalOutlook(abroad.getAbdIdentity());
            partyFabroad.setDestination(abroad.getAbdDestination()+""+abroad.getAbdCity());
            partyFabroad.setFromStart(abroad.getAbdBeginDate());
            partyFabroad.setFromEnd(abroad.getAbdEndDate());
            partyFabroad.setDomesticTell(abroad.getAbdTelWithIn());
            partyFabroad.setAbroadTell(abroad.getAbdTelWithOut());
            partyFabroad.setFundingChannel(abroad.getAbdFunds());
            partyFabroad.setMethod(abroad.getAbdTripMode());
            partyFabroad.setRemarks(abroad.getAbdRemark());
            partyFabroad.setAbdStatus(1);
            long date = new Date().getTime();
            String s = String.valueOf(date);
            partyFabroad.setTime(s);



            //亲属团
            map.put("code",0);
            for (int i=0;i<abroad.getInName1().size();i++){
                //出国成员关系表
                PartyFaboradFamily partyFaboradFamily = new PartyFaboradFamily();
                partyFaboradFamily.setFaboradId(s);
                partyFaboradFamily.setName(abroad.getInName1().get(i).replace("\"","").replace("[","").replace("]",""));
                partyFaboradFamily.setRelationship(abroad.getInShip1().get(i).replace("\"","").replace("[","").replace("]",""));
                partyFaboradFamily.setDomesticAborad("国内亲属");
                partyFaboradFamily.setCompany(abroad.getInJob1().get(i).replace("\"","").replace("[","").replace("]",""));
                partyFaboradFamily.setTime(s);
                saveService.save(partyFaboradFamily);
            }

            for (int i=0;i<abroad.getOutName1().size();i++){
                //出国成员关系表
                PartyFaboradFamily partyFaboradFamily = new PartyFaboradFamily();
                partyFaboradFamily.setFaboradId(s);
                partyFaboradFamily.setName(abroad.getOutName1().get(i).replace("\"","").replace("[","").replace("]",""));
                partyFaboradFamily.setRelationship(abroad.getOutShip1().get(i).replace("\"","").replace("[","").replace("]",""));
                partyFaboradFamily.setCompany(abroad.getOutJob1().get(i).replace("\"","").replace("[","").replace("]",""));
                partyFaboradFamily.setDomesticAborad("国外亲属");
                partyFaboradFamily.setTime(s);
                saveService.save(partyFaboradFamily);
            }
            saveService.save(partyFabroad);


        }

        return map;
    }


    /**
     **
      *@author:WY
      *@date: 2018/7/21 20:09
      *@description:举报个人
     */

    @PostMapping("/reportperson")
    @ResponseBody
    public Map<String,Object> getRperson(ReportPerson reportPerson){
        Map<String,Object> map = new HashMap<>();

        long date = new Date().getTime();
        String s = String.valueOf(date);
        reportPerson.setTime(s);

        saveService.save(reportPerson);
        map.put("code",0);
        return map;
    }

    /**
     * *
      *@author:WY
      *@date: 2018/7/21 20:29
      *@description:举报组织
     *
     */
    @PostMapping("/reportgroup")
    @ResponseBody
    public Map<String,Object> getRgroup(ReportGroup reportGroup1){
        Map<String,Object> map = new HashMap<>();
        long date = new Date().getTime();
        String s = String.valueOf(date);
        reportGroup1.setTime(s);


        reportGroupService.saveAll(reportGroup1);
        map.put("code",0);
        map.put("searchCode",s);

        return map;
    }

    /**
     **
      *@author:WY
      *@date: 2018/7/23 18:39
      *@description:得到指定文章
     */
    @PostMapping("/getarticle")
    @ResponseBody
    public Map<String,Object> getArticles(@Param("id") int id ){
        Map<String,Object> map = new HashMap<>();
        PubArticle pubArticle = pubArticleService.findById(id);
        map.put("article",pubArticle);
        return map;
    }

    /**
     **
      *@author:WY
      *@date: 2018/7/24 17:34
      *@description:保存入党申请内容
     */
    @PostMapping("/applyparty")
    @ResponseBody
    public Map<String,Object> applyParty(PartyApply partyApply){
        Map<String,Object> map = new HashMap<>();
        saveService.save(partyApply);
        System.out.println(partyApply.getIcon());
        map.put("code",0);
        return map;
    }
    /**
     * @author  王宇
     * @date 2018/7/28
     * @description  获取党建要闻文章
     */
    @PostMapping("/newsarticle")
    @ResponseBody
    public Map<String,Object> newsList(@Param("check") int check){
        Map<String,Object> map = new HashMap<>();

        if(check==1){
            List<PubArticle> articleList = pubArticleService.findPubArticlesByType("党建要闻");
            map.put("data",articleList);

        }else if(check==2){
            List<PubArticle> articleList2 = pubArticleService.findPubArticlesByType("实时动态");
            map.put("data",articleList2);

        }else if(check==3){
            List<PubArticle> articleList3 = pubArticleService.findPubArticlesByType("政治思想建设");
            map.put("data",articleList3);

        }else if(check==4){
            List<PubArticle> articleList4 = pubArticleService.findPubArticlesByType("通知公告");
            map.put("data",articleList4);
        }else {
            map.put("code",1);
        }
        return map;
    }
    /**
     * @author  王宇
     * @date 2018/8/15
     * @description  拿到视频文件
     */
    @PostMapping("/getvideos")
    @ResponseBody
    public Map<String,Object> getVideos(@Param("id") int id){
        Map<String,Object> map = new HashMap<>();
        //通过课程id  拿到对应的videos
        List<PartyVideo> videos = videoService.findByLessonId(id);
        //根据课程id 获取课程名称
        PartyLesson lesson = lessonService.findById(id);
        String lessonName = lesson.getName();
        map.put("code",0);
        map.put("data",videos);
        map.put("lesson",lessonName);

        return map;
    }

}

